package edu.usc.libtalk;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import edu.usc.libtalk.task.GetJSONObjectTask;
import edu.usc.libtalk.utils.ApplicationService;
import edu.usc.libtalk.utils.InternalConstant;

public class SignInActivity extends Activity implements InternalConstant {
	private String library;
	private Intent intent;
	private static final String TAG = "SignInActivity";
	public static Activity signinActivity;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Customize menu title 
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        
        setContentView(R.layout.activity_sign_in);
        
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
        
        // Help button Handler    	
    	Button help_bn = (Button) findViewById(R.id.help_button);
    	help_bn.setOnClickListener(new OnClickListener() {
    		public void onClick(View v) {
    			helpDialog();
    		}
    	});    	
        
       
        
        signinActivity = this;
        
        library = ApplicationService.getLibrary();
        String message = "Current library: " + library;
        
        TextView tv = (TextView) findViewById(R.id.sigin_lib_name);
        tv.setText(message);        
    }
    
    /* Called when the user clicks the "Anonymous" button */
    public void anonymousLoginHandler(View view) {
		// Store user name as default name
		ApplicationService.setUserName(DEFAULT_USERNAME);
    	
    	intent = new Intent(SignInActivity.this, ChatActivity.class);            	    	    
    	
       	// Register the user with default name    	
    	new GetJSONObjectTask(){
    		@Override
    		protected void onPostExecute(JSONObject result){
				if(result == null){
					ApplicationService.inform(getString(R.string.nointernet));
					finish();
					return;
				}         		
				
				// Parse JSON Object				
                try{
                	String authen = result.getString("result");
                	
                	if((authen.length() != 0) && authen.equals("Success"))
                	{                		
                		
                		startActivity(intent);
                	}else {
                		ApplicationService.inform("Connection errored");
    					finish();
    					return;                		
                	}
                	
                	
                }
                catch(JSONException e){
                	Log.e(TAG, "Error parsing data "+e.toString());
                }         		
         	}	
        }.execute(HOSTNAME + AUTHENTICATION_URL + "?devicetoken=" + ApplicationService.getDeviceID() + "&library=-1&level=-1&seat=-1&name=" + ApplicationService.getUserName()+"&interest=-1");    	
    }
    
    /* Called when the user clicks the "Locate yourself" button */
    public void loginHandler(View view) {
    	Intent intent = new Intent(SignInActivity.this, SignInWithNameActivity.class);            	
    	startActivity(intent);    	
    } 
    
    private void helpDialog()
    {
    	AlertDialog alertDialog = new AlertDialog.Builder(SignInActivity.this).create();
		
		// Setting Dialog Title
		alertDialog.setTitle("Help");
		
		// Setting Dialog Message
		alertDialog.setMessage("Step 1 : Select the Library.\n\nStep 2 : Login as Registered (with Name and Interest) or Anonymous User.\n\nStep 3 : If you are Registered user, then select your seat (free spot) on any level of that selected library.\n\nStep 4 : Click on the \"online users\" tab to see all the online users of that particular library with their \"Area of Interest\". Click any one from the list and start chat.\n\nStep 5 : You can also go to all the levels and can select users and start chat.\n\nStep 6 : After you are done chatting, click on the \"Sign out\" button on the header. Here you can also send the entire chat of your message board to your email.\n\nStep 7 : Happy Chatting ...");

		// Setting OK Button
		alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int which) {
		    	// Write your code here to execute after dialog closed
		    		
		    }
		});
		
		alertDialog.show();
    }
}
